package com.example.tallybook.ui.screens

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.tallybook.data.TransactionType
import com.example.tallybook.ui.components.*
import com.example.tallybook.ui.viewmodel.TransactionViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeScreen(
    viewModel: TransactionViewModel = hiltViewModel()
) {
    val uiState by viewModel.uiState.collectAsState()
    var showAddDialog by remember { mutableStateOf(false) }
    var searchQuery by remember { mutableStateOf("") }
    var selectedFilter by remember { mutableStateOf<TransactionType?>(null) }

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("记账本", fontWeight = FontWeight.Bold) },
                actions = {
                    IconButton(onClick = { viewModel.insertSampleData() }) {
                        Icon(Icons.Default.Refresh, contentDescription = "添加示例数据")
                    }
                }
            )
        },
        floatingActionButton = {
            FloatingActionButton(onClick = { showAddDialog = true }) {
                Icon(Icons.Default.Add, contentDescription = "添加交易")
            }
        }
    ) { paddingValues ->
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues)
                .padding(horizontal = 16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            item {
                Spacer(modifier = Modifier.height(8.dp))
                
                // 统计信息
                StatsRow(
                    totalIncome = uiState.totalIncome,
                    totalExpense = uiState.totalExpense,
                    balance = uiState.balance
                )
                
                Spacer(modifier = Modifier.height(16.dp))
                
                // 筛选按钮
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    FilterChip(
                        onClick = { 
                            selectedFilter = null
                            viewModel.filterByType(null)
                        },
                        label = { Text("全部") },
                        selected = selectedFilter == null,
                        modifier = Modifier.weight(1f)
                    )
                    FilterChip(
                        onClick = { 
                            selectedFilter = TransactionType.INCOME
                            viewModel.filterByType(TransactionType.INCOME)
                        },
                        label = { Text("收入") },
                        selected = selectedFilter == TransactionType.INCOME,
                        modifier = Modifier.weight(1f)
                    )
                    FilterChip(
                        onClick = { 
                            selectedFilter = TransactionType.EXPENSE
                            viewModel.filterByType(TransactionType.EXPENSE)
                        },
                        label = { Text("支出") },
                        selected = selectedFilter == TransactionType.EXPENSE,
                        modifier = Modifier.weight(1f)
                    )
                }
                
                Spacer(modifier = Modifier.height(16.dp))
                
                Text(
                    text = "交易记录",
                    style = MaterialTheme.typography.titleLarge,
                    fontWeight = FontWeight.Bold
                )
            }
            
            if (uiState.transactions.isEmpty()) {
                item {
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(vertical = 32.dp),
                        contentAlignment = Alignment.Center
                    ) {
                        Text(
                            text = "暂无交易记录",
                            style = MaterialTheme.typography.bodyLarge,
                            color = MaterialTheme.colorScheme.onSurfaceVariant
                        )
                    }
                }
            } else {
                items(uiState.transactions) { transaction ->
                    TransactionCard(
                        transaction = transaction,
                        onDelete = { viewModel.deleteTransaction(transaction) }
                    )
                }
            }
        }
    }
    
    if (showAddDialog) {
        AddTransactionDialog(
            onDismiss = { showAddDialog = false },
            onConfirm = { amount, description, category, type, note ->
                viewModel.addTransaction(amount, description, category, type, note)
            }
        )
    }
} 